import React from 'react';

class Example extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            count: 0
        };
        this.componentDidUpdate = this.componentDidUpdate.bind(this);
        this.add = this.add.bind(this);
    }

    componentDidUpdate() {
        setTimeout(() => {
            console.log(this.state);
        }, 3000);
    }

    add() {
        const { count } = this.state;
        this.setState({ count: count + 1 });
    }

    // 同样的操作，打印出的结果是 5 5 5 5 5

    render() {
        return (
            <div>
                <button type="button" onClick={this.add}>
                    click me
                </button>
            </div>
        );
    }
}
export default Example;
